package NodeList;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class removeNthFromEnd {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode node = head;
        int size = 0;
        int search = 0;
        while (node != null){
            size++;
            node = node.next;
        }
        if (n == size && head != null){
            return head.next;
        }
        node = head;
        while (node != null){
            search++;
            if (search == size-n){
                node.next = node.next.next;
            }
            node = node.next;
        }
        return head;
    }
}
